﻿#include "unrealircd.h"
#include<stdlib.h>
CMD_FUNC(cmd_ThirdLogin);
CMD_FUNC(cmd_thirdOpenIdAdd);
CMD_FUNC(cmd_ThirdDel);
void ThirdSendUrl(Client *client,ThirdAPPID thirdApp,ThirdStatus thirdstatus);
WEBFUNC(webQQCallback);
WEBFUNC(webWechatCallback);
ThirdLogin *thirdLogin_list_add(Client *client,ThirdAPPID thirdApp,ThirdStatus thirdStatus);
ThirdLogin *thirdLogin_list_Get(int thirdApp,char *statueCode);
HTTPREQUESTSUCCESSCALLBACK(web_qq_get_access_token);
HTTPREQUESTSUCCESSCALLBACK(web_qq_get_appid);
HTTPREQUESTSUCCESSCALLBACK(web_wechat_get_appid);
HTTPREQUESTFAILCALLBACK(web_wechat_get_appid_faile);
HTTPREQUESTFAILCALLBACK(web_qq_get_access_token_and_appid_faile);
void _ThirdLogin_free(Client *client,char *statueCode);
void loginOrRegister(Client *client,ThirdAPPID thirdApp,char * openid);
void thirdAppAdd(Client *client,ThirdAPPID thirdApp,char * openid);
void thirdAppModify(Client *client,ThirdAPPID thirdApp,char * openid);
char **get_wechat_head(int *head_num);
IMConfigureSave thirdloginConfigure_save;

ModuleHeader MOD_HEADER
= {
	"third/thirdlogin",
	"1.1",
	"third user to login",
	"waitch",
	"unrealircd-5",
};

ThirdLogin *thirdLogin;
MOD_TEST(){
	MARK_AS_OFFICIAL_MODULE(modinfo);
	EfunctionAddVoid(modinfo->handle, EFUNC_THIRDLOGIN_FREE, _ThirdLogin_free);
	return MOD_SUCCESS;
}

MOD_INIT()
{
	thirdLogin=NULL;
	CommandAdd(modinfo->handle, "THIRDLOGIN", cmd_ThirdLogin, 1, CMD_UNLOGIN);
	CommandAdd(modinfo->handle, "THIRDADD", cmd_thirdOpenIdAdd, 1, CMD_USER);
	CommandAdd(modinfo->handle, "THIRDDEL", cmd_ThirdDel, 1, CMD_USER);
	urlFuncAdd("/api/qqlogin/callback",webQQCallback,REQUEST_GET|REQUEST_POST);
	urlFuncAdd("/api/wechatlogin/callback",webWechatCallback,REQUEST_GET|REQUEST_POST);
	return MOD_SUCCESS;
}

MOD_LOAD()
{
	return MOD_SUCCESS;
}

MOD_UNLOAD()
{
	return MOD_SUCCESS;
}

/**
 * 获取发送的stat标识，和appid
 * parv[1]:第三方标识，例如0代表qq，1代表微信 
 */
CMD_FUNC(cmd_ThirdLogin){
	if (!IsAPPIdent(client)) {
		sendnumeric(client, ERR_WHITHOUTAPPIDENT);
		return;
	}
	if (parc != 2 || BadPtr(parv[1])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "ThirdLOGIN");
		return;
	}
	if(!is_digist_string(parv[1],strlen(parv[1]))){
		sendnumeric(client, RIGISTERPARAMNOTRIGHT, "thridAPP");
		return;
	}
	int thirdApp=atoi(parv[1]);
	switch (thirdApp)
	{
	case THIRDAPPID_QQ:
		break;
	case THIRDAPPID_WECHAT:
		break;
	default:
		sendnumeric(client, RIGISTERPARAMNOTRIGHT, "thridAPP");
		return;
	}
	ThirdSendUrl(client,thirdApp,THIRDSTATUS_LOGIN);
}

/**
 * 获取发送的stat标识，和appid
 * parv[1]:第三方标识，例如0代表qq，1代表微信 
 */
CMD_FUNC(cmd_thirdOpenIdAdd){
	if (!IsAPPIdent(client)) {
		sendnumeric(client, ERR_WHITHOUTAPPIDENT);
		return;
	}
	if (parc != 2 || BadPtr(parv[1])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "ThirdLOGIN");
		return;
	}
	if(!is_digist_string(parv[1],strlen(parv[1]))){
		sendnumeric(client, RIGISTERPARAMNOTRIGHT, "thridAPP");
		return;
	}
	int thirdApp=atoi(parv[1]);
	switch (thirdApp)
	{
	case THIRDAPPID_QQ:
		break;
	case THIRDAPPID_WECHAT:
		break;
	default:
		sendnumeric(client, RIGISTERPARAMNOTRIGHT, "thridAPP");
		return;
	}
	ThirdSendUrl(client,thirdApp,THIRDSTATUS_ADD);
}

void ThirdSendUrl(Client *client,ThirdAPPID thirdApp,ThirdStatus thirdstatus){
	ThirdLogin *newThirdLogin=thirdLogin_list_add(client,thirdApp,thirdstatus);
	char url[2083]="";
	int configure_done=1;
	IMConfigureSave *appid_configure,*redirectUrl_configure;
	switch(newThirdLogin->thridAPP){
		case THIRDAPPID_QQ:
			appid_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_QQ_APPID");
			if(!appid_configure){
				sendnumeric(client, CONFIGUREFAILEBYLACKMESSAGE, "THIRDLOGIN","THIRD_QQ_APPID");
				configure_done=0;
				break;
			}
			redirectUrl_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_QQ_REDIRECT_URL");
			if(!redirectUrl_configure){
				sendnumeric(client, CONFIGUREFAILEBYLACKMESSAGE, "THIRDLOGIN","THIRD_QQ_REDIRECT_URL");
				configure_done=0;
				break;
			}
			
			sprintf(url,"https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s",appid_configure->configure_value,redirectUrl_configure->configure_value,newThirdLogin->statueCode);
			break;
		case THIRDAPPID_WECHAT:
			appid_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_WECHAT_APPID");
			if(!appid_configure){
				sendnumeric(client, CONFIGUREFAILEBYLACKMESSAGE, "THIRDLOGIN","THIRD_WECHAT_APPID");
				configure_done=0;
				break;
			}
			redirectUrl_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_WECHAT_REDIRECT_URL");
			if(!redirectUrl_configure){
				sendnumeric(client, CONFIGUREFAILEBYLACKMESSAGE, "THIRDLOGIN","THIRD_WECHAT_REDIRECT_URL");
				configure_done=0;
				break;
			}
			sprintf(url,"https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect",appid_configure->configure_value,redirectUrl_configure->configure_value,newThirdLogin->statueCode);
			break;
		default:
			configure_done=0;
			break;

	}
	if(configure_done){
		sendnumeric(client, THIRDLOGINSUCCESS, newThirdLogin->thridAPP,url);
	}
	else{
		_ThirdLogin_free(client,newThirdLogin->statueCode);
	}

	return;
}

/**
 * 向thirdLogin链表中添加数据
 */
ThirdLogin *thirdLogin_list_add(Client *client,ThirdAPPID thirdApp,ThirdStatus thirdStatus){
	ThirdLogin *temp=thirdLogin;
	ThirdLogin *lasttemp=thirdLogin;
	char *stataCode=NULL;
	//随机值不能重复
	do{
		stataCode=random_digist(10);
		ThirdLogin *third=thirdLogin;
		while(third){
			if(!strcmp(stataCode,third->statueCode)){
				safe_free(stataCode);
				break;
			}
			third=third->next;
		}
	}while(!stataCode);

	while(temp){
		if(temp->client==client){
			break;
		}
		lasttemp=temp;
		temp=temp->next;
	}
	ThirdLogin *newThirdLogin;
	if(temp){
		newThirdLogin=temp;
		ircd_log(LOG_ERROR,"third_find old %s",newThirdLogin->statueCode);
		safe_free(newThirdLogin->statueCode);
		newThirdLogin->statueCode=stataCode;
		newThirdLogin->thirdStatus=thirdStatus;
		newThirdLogin->thridAPP=thirdApp;
		newThirdLogin->begin_login_time=time(NULL);
	}
	else{
		newThirdLogin=safe_alloc(sizeof(ThirdLogin));
		if(lasttemp){
			newThirdLogin->next=NULL;
			newThirdLogin->pre=lasttemp;
			lasttemp->next=newThirdLogin;
		}
		else{
			newThirdLogin->next = NULL;
			newThirdLogin->pre = NULL;
			thirdLogin = newThirdLogin;
		}
		newThirdLogin->statueCode = stataCode;
		newThirdLogin->thridAPP = thirdApp;
		newThirdLogin->thirdStatus = thirdStatus;
		newThirdLogin->begin_login_time = time(NULL);
		newThirdLogin->client = client;
	}
	return newThirdLogin;
}

/**
 * 查找thirdLogin链表中的statueCode值是否存在
 */
ThirdLogin *thirdLogin_list_Get(int thirdApp,char *statueCode){
	if(statueCode==NULL){
		return NULL;
	}
	ThirdLogin *temp=thirdLogin;
	while(temp){
		if(!strcmp(statueCode,temp->statueCode)&&temp->thridAPP==thirdApp){
			break;
		}
		temp=temp->next;
	}
	return temp;
}



/**
 * QQ第三方登录的回调接口
 */
WEBFUNC(webQQCallback){
	web_set_httpResponseBody(web_user,"<body onload='setTimeout(\"mm()\",1000)'><script>function mm(){ window.opener=window;var win = window.open(\"about:blank\",\"_self\");win.close();} </script></body>");
	RequestData *requestData=web_user->requestData;
	UrlParamList *codeParam=web_request_get_paramList(requestData,"code");
	if(!codeParam){
		return;
	}
	UrlParamList *stateParam=web_request_get_paramList(requestData,"state");
	if(!stateParam){
		return;
	}
	ircd_log(LOG_ERROR,"QQ callback code:%s,statue:%s",codeParam->param_value,stateParam->param_value);
	ThirdLogin *third=thirdLogin_list_Get(THIRDAPPID_QQ,stateParam->param_value);
	if(!third){
		return;
	}
	if(IsDeadSocket(third->client)){
		_ThirdLogin_free(third->client,stateParam->param_value);
		return;
	}
	char url[2083]="";
	IMConfigureSave *appid_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_QQ_APPID");
	if(!appid_configure){
		return;
	}
	IMConfigureSave *redirectUrl_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_QQ_REDIRECT_URL");
	if(!redirectUrl_configure){
		return;
	}
	IMConfigureSave *appid_secret_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_QQ_APPID_SECRET");
	if(!appid_secret_configure){
		return;
	}
	sprintf(url,"https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s&fmt=json",
		appid_configure->configure_value,
		appid_secret_configure->configure_value,
		codeParam->param_value,
		redirectUrl_configure->configure_value
	);
	HttpRequestData * data = malloc(sizeof(HttpRequestData));
	data->url=NULL;
	safe_strdup(data->url,url);
	data->data=NULL;
	data->successfinishCallFun=web_qq_get_access_token;
	data->failefinishCallFun=web_qq_get_access_token_and_appid_faile;
	data->client=third->client;
	data->requestType = 0;
	data->userPoint=third;
	data->head_num=0;
	add_httpRequestReady(data);
}

HTTPREQUESTSUCCESSCALLBACK(web_qq_get_access_token){
	ircd_log(LOG_ERROR,"web_qq_get_access_tokens,httpCode:%d,requestData->response:%s",httpCode,requestData->response);
	switch(httpCode){
		case 200:
			{
				cJSON *cjson;
				cjson=cJSON_Parse(requestData->response);
				cJSON *access_token_cjson=cJSON_GetObjectItem(cjson,"access_token");
				if(!access_token_cjson){
					sendnumeric(client, THIRDLOGINFAILE, "Get QQ access_token faile");
					break;
				}
				char *access_token=access_token_cjson->valuestring;
				HttpRequestData * httpdata = malloc(sizeof(HttpRequestData));
				httpdata->url=NULL;
				char url[2083]="";
				sprintf(url,"https://graph.qq.com/oauth2.0/me?access_token=%s&fmt=json",access_token);
				safe_strdup(httpdata->url,url);
				httpdata->data=NULL;
				httpdata->successfinishCallFun=web_qq_get_appid;
				httpdata->failefinishCallFun=web_qq_get_access_token_and_appid_faile;
				httpdata->client=client;
				httpdata->requestType = 0;
				httpdata->userPoint=data;
				httpdata->head_num=0;
				add_httpRequestReady(httpdata);
				break;
			}
		default:
			sendnumeric(client, THIRDLOGINFAILE, "Get QQ access_token faile");
			break;
	}
	
}

HTTPREQUESTFAILCALLBACK(web_qq_get_access_token_and_appid_faile){
	sendnumeric(client, THIRDLOGINFAILE, "Get qq openid faile");
	return;
}

HTTPREQUESTSUCCESSCALLBACK(web_qq_get_appid){
	ircd_log(LOG_ERROR,"web_qq_get_appid,httpCode:%d,requestData->response:%s",httpCode,requestData->response);
	ThirdLogin *third=(ThirdLogin*)data;
	ThirdStatus thirdStatus=third->thirdStatus;
	_ThirdLogin_free(third->client,third->statueCode);
	switch(httpCode){
		case 200:
			{
				cJSON *cjson;
				cjson=cJSON_Parse(requestData->response);
				cJSON *openid_cjson=cJSON_GetObjectItem(cjson,"openid");
				if(!openid_cjson){
					sendnumeric(client, THIRDLOGINFAILE, "Get QQ openid faile");
					break;
				}
				char openid[200];
				strcpy(openid,openid_cjson->valuestring);
				cJSON_Delete(cjson);

				switch(thirdStatus){
					case THIRDSTATUS_LOGIN:
						loginOrRegister(client,THIRDAPPID_QQ,openid);
						break;
					case THIRDSTATUS_ADD:
						thirdAppAdd(client,THIRDAPPID_QQ,openid);
						break;
					case THIRDSTATUS_MODIFY:
						thirdAppModify(client,THIRDAPPID_QQ,openid);
						break;
				}
				break;
			}
		default:
			sendnumeric(client, THIRDLOGINFAILE, "Get QQ openid faile");
			break;
	}
	
}

/**
 * @brief 第三方微信的回调接口
 * 
 */
WEBFUNC(webWechatCallback){
	web_set_httpResponseBody(web_user,"<body onload='setTimeout(\"mm()\",1000)'><script>function mm(){ window.opener=window;var win = window.open(\"about:blank\",\"_self\");win.close();} </script></body>");
	RequestData *requestData=web_user->requestData;
	UrlParamList *codeParam=web_request_get_paramList(requestData,"code");
	if(!codeParam){
		return;
	}
	UrlParamList *stateParam=web_request_get_paramList(requestData,"state");
	if(!stateParam){
		return;
	}
	ircd_log(LOG_ERROR,"wechat callback code:%s,statue:%s",codeParam->param_value,stateParam->param_value);
	ThirdLogin *third=thirdLogin_list_Get(THIRDAPPID_WECHAT,stateParam->param_value);
	if(!third){
		return;
	}
	if(IsDeadSocket(third->client)){
		_ThirdLogin_free(third->client,stateParam->param_value);
		return;
	}
	char url[2083]="";
	IMConfigureSave *appid_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_WECHAT_APPID");
	if(!appid_configure){
		return;
	}
	IMConfigureSave *appid_secret_configure=D_FindImConfigureSaveNode(&thirdloginConfigure_save,"THIRD_WECHAT_APPID_SECRET");
	if(!appid_secret_configure){
		return;
	}
	sprintf(url,"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
		appid_configure->configure_value,
		appid_secret_configure->configure_value,
		codeParam->param_value
	);
	HttpRequestData * data = malloc(sizeof(HttpRequestData));
	safe_strdup(data->url,url);
	data->data=NULL;
	data->successfinishCallFun=web_wechat_get_appid;
	data->failefinishCallFun=web_wechat_get_appid_faile;
	data->client=third->client;
	data->requestType = 0;
	data->userPoint=third;
	//data->head = get_wechat_head(&data->head_num);
	data->head_num=0;
	add_httpRequestReady(data);
}

char **get_wechat_head(int *head_num) {
	*head_num = 1;
	char **head = malloc((*head_num) * sizeof(char *));
	safe_strdup(head[0],"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36");
	return head;
}

HTTPREQUESTSUCCESSCALLBACK(web_wechat_get_appid){
	ircd_log(LOG_ERROR,"web_wechat_get_appid,httpCode:%d,requestData->response:%s",httpCode,requestData->response);
	ThirdLogin *third=(ThirdLogin*)data;
	ThirdStatus thirdStatus=third->thirdStatus;
	_ThirdLogin_free(third->client,third->statueCode);
	switch(httpCode){
		case 200:
			{
				cJSON *cjson;
				cjson=cJSON_Parse(requestData->response);
				cJSON *openid_cjson=cJSON_GetObjectItem(cjson,"openid");
				if(!openid_cjson){
					sendnumeric(client, THIRDLOGINFAILE, "Get wechat openid faile");
					break;
				}
				char openid[200];
				strcpy(openid,openid_cjson->valuestring);
				cJSON_Delete(cjson);

				switch(thirdStatus){
					case THIRDSTATUS_LOGIN:
						loginOrRegister(client,THIRDAPPID_WECHAT,openid);
						break;
					case THIRDSTATUS_ADD:
						thirdAppAdd(client,THIRDAPPID_WECHAT,openid);
						break;
					case THIRDSTATUS_MODIFY:
						thirdAppModify(client,THIRDAPPID_WECHAT,openid);
						break;
				}
				break;
			}
		default:
			sendnumeric(client, THIRDLOGINFAILE, "Get wechat openid faile");
			break;
	}
	
}

HTTPREQUESTFAILCALLBACK(web_wechat_get_appid_faile){
	sendnumeric(client, THIRDLOGINFAILE, "Get wechat openid faile");
	return;
}



void _ThirdLogin_free(Client *client,char *statueCode){
	ThirdLogin *temp=thirdLogin;
	ThirdLogin *now=NULL;
	while(temp){
		if(temp->client==client||(statueCode&&!strcmp(statueCode,temp->statueCode))){
			now=temp;
			break;
		}
		temp=temp->next;
	}
	if(now){
		if(now==thirdLogin){
			thirdLogin=thirdLogin->next;
		}
		if(now->next){
			now->next->pre=now->pre;
		}
		if(now->pre){
			now->pre->next=now->next;
		}
		safe_free(now->statueCode);
		safe_free(now);
	}

}


void loginOrRegister(Client *client,ThirdAPPID thirdApp,char * openid){
	int thirdLogin_len=0;
	D_third_login *third_data=queryImThirdLoginByOpenidAndThirdid(openid,thirdApp,&thirdLogin_len);
	if(thirdLogin_len==0){//获得到openid，但是没有这个qq用户未注册IM账号，需要重新注册一个账号
		sendnumeric(client, THIRDLOGINNEEDREGISTER, openid,thirdApp);
		db_D_ThirdLoginFree(third_data,thirdLogin_len);
		return;
	}
	else if(thirdLogin_len==1){//获得到openid，并且搜索到当前用户，可以登陆
		int len=0;
		D_User *user=queryImUserById(*(third_data->user_id),&len);
		db_D_ThirdLoginFree(third_data,thirdLogin_len);
		if(len!=1){
			sendnumeric(client, THIRDLOGINFAILE, "query database faile");
			return;
		}
		
		//在unrealIRCD中登录这个用户
		make_user(client);
		userLoginLog(client->user_id,client->ip,client->mac);
		/* set::modes-on-connect */
		client->umodes |= CONN_MODES;
		client->user->server = me_hash;
		strlcpy(client->info, int_to_string(*(user->id)), sizeof(client->info));
		strlcpy(client->user->username, user->user_name, USERLEN + 1);
		char nick_name[51];
		strlcpy(nick_name, user->user_name, USERLEN + 1);
		client->user_id = *(user->id);
		int nick_result=nick_set(client, nick_name);
		if (nick_result) {
			return;
		}
		register_user(client, client->name, user->user_name, NULL, NULL, NULL);
		db_D_UserFree(user,len);
	}
	else{
		sendnumeric(client, SQLERROR, "query database faile");
	}
}

/**
 * @brief 更改第三方用户的openid或者新增一个第三方openid
 * 
 * @param client 连接的客户端
 * @param thirdApp 表示第三方的id
 * @param openid 第三方的用户openid
 */
void thirdAppAdd(Client *client,ThirdAPPID thirdApp,char * openid){
	int thirdLogin_len=0;
	D_third_login *third_data=queryImThirdLoginByUseridAndThirdid(client->user_id,thirdApp,&thirdLogin_len);
	if(thirdLogin_len==1){
		sendnumeric(client, THIRDOPENADDFAILE, "The user already has a third openid");
	}
	else if(thirdLogin_len==0){
		int thirdLoginOpenid_len=0;
		D_third_login *thirdopenid_data=queryImThirdLoginByOpenidAndThirdid(openid,thirdApp,&thirdLoginOpenid_len);
		if(thirdLoginOpenid_len == 0){
			D_third_login *third_data=db_createThirdLogin();
			safe_strdup(third_data->nick_name,client->user->username);
			safe_strdup(third_data->open_id,openid);
			third_data->third_id=safe_alloc(sizeof(int));
			*(third_data->third_id)=thirdApp;
			third_data->user_id=safe_alloc(sizeof(int));
			*(third_data->user_id)=client->user_id;
			int third_statue=0;
			char insert_error_message1[200]=""; 
			
			D_third_login *tempThirdLogin=insertImThirdLogin(third_data,&third_statue,insert_error_message1);
			if(third_statue==-1){
				sendnumeric(client, SQLERROR, "insert database faile");
			}
			else{
				sendnumeric(client, THIRDOPENADDSUCCESS);
			}
			db_D_ThirdLoginFree(third_data,thirdLogin_len);
			db_D_ThirdLoginFree(tempThirdLogin,third_statue);
		}
		else{
			if(thirdLoginOpenid_len == 1){
				sendnumeric(client, THIRDOPENDIDEXISTED);
			}
			else{
				sendnumeric(client, SQLERROR, "query database faile");
			}
		}
		db_D_ThirdLoginFree(thirdopenid_data,thirdLoginOpenid_len);
	}
	else{
		sendnumeric(client, SQLERROR, "query database faile");
	}
	db_D_ThirdLoginFree(third_data,thirdLogin_len);
}

/**
 * @brief 修改用户连接的第三方的openid，待完善
 * 
 * 
 * @param client 连接的客户端
 * @param thirdApp 表示第三方的id
 * @param openid 第三方的用户openid
 */
void thirdAppModify(Client *client,ThirdAPPID thirdApp,char * openid){

}


/**
 * 获取发送的stat标识，和appid
 * parv[1]:第三方标识，例如0代表qq，1代表微信 
 */
CMD_FUNC(cmd_ThirdDel){
	if (parc != 2 || BadPtr(parv[1])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "THIRDDEL");
		return;
	}

	if(!is_digist_string(parv[1],strlen(parv[1]))){
		sendnumeric(client, RIGISTERPARAMNOTRIGHT, "THIRDDEL");
		return;
	}
	int thirdApp=atoi(parv[1]);
	switch (thirdApp)
	{
	case THIRDAPPID_QQ:
	case THIRDAPPID_WECHAT:
		break;
	default:
		sendnumeric(client, RIGISTERPARAMNOTRIGHT, "thridAPP");
		return;
	}
	int datalen =0;
	D_third_login *third=deleteImThirdLoginByUseridAndThirdId(client->user_id,thirdApp,&datalen);
	db_D_ThirdLoginFree(third,datalen);
	if(datalen==-1){
		sendnumeric(client, THIRDDELFAILE, "database faile");
		return;
	}
	else{
		sendnumeric(client, THIRDDELSUCCESS);
		return;
	}

}